<?php
class CommentModel extends MY_Model{
    public $table = 't_comment';
    public function __construct(){
        parent::__construct();
    }
    /**
     * 获取评论列表
     * name: getCommentList
     * @param array $data where条件
     * @param int $page
     * @param int $pagesize
     * @return array
     * @throws Exception
     * @Author:taiyang
     */
    public function getCommentList($data=array(),$page=1,$pagesize=10,$orderby=''){
//        $data='';
        $sql=$this->CommentSql($data);
//        echo $sql;exit;
        if(!empty($orderby)){
            $sql.=" order by ".$orderby." ";
        }
        $index = $page - 1 < 0 ? 0 : ($page - 1) * $pagesize;
        $sql.=" limit ".$index.",".$pagesize."";
        $res=$this->query_sql_list($sql);
        return $res;
    }

    /**
     * 方法名
     * name: getCommentNum
     * @param array $data
     * @return int
     * @throws Exception
     * @Author:taiyang
     */
    public function getCommentNum($data=array()){
        $sql="select count(*) from ".$this->table." tc left join t_post p on p.id=tc.post_id left join t_user u on u.uid=tc.uid where 1 ";
        $sql.=$this->getWhereSql($data);
//                echo $sql;exit;
        $num=$this->query_sql_count($sql);
        return $num;
    }

    /**
     * 生成sql
     * name: CommentSql
     * @param array $data
     * @return string
     * @Author:taiyang
     */
    public function CommentSql($data=array()){
        $sql="select u.sex,u.user_source,u.nickname,u.user_type,p.title as p_title,p.status as p_status,tc.* from ".$this->table." tc 
        left join t_post p on p.id=tc.post_id 
        left join t_user u on u.uid=tc.uid where 1 ";
        $sql.=$this->getWhereSql($data);
        return $sql;
    }

    /**
     * 生成where条件
     * name: getWhereSql
     * @param array $data
     * @return string
     * @Author:taiyang
     */
    public function getWhereSql($data=array()){
        $sql='';
        if(empty($data)){return $sql;}
        if(!empty($data['post_id'])){
            $sql.=" and tc.post_id=".$data['post_id']." ";
        }
        if(!empty($data['s_content'])){
            $sql.=$this->getSqlLike($data);
        }
        if(!empty($data['post_type'])){
            $sql.=$this->getSqlType($data);
        }
        if(!empty($data['status'])){
            $sql.=" and tc.status = ".$data['status']." ";
        }
        if(!empty($data['is_audio'])){
            $is_audio=($data['is_audio'] == 1?">":"<=");
            $sql.=" and tc.audio_id ".$is_audio." 0 ";
        }
        if(!empty($data['is_audit'])){
            if($data['is_audit'] == 2){
                $data['is_audit']="0,2";
            }
            $sql.=" and tc.is_audit in (".$data['is_audit'].") ";
        }

        return $sql;

    }

    /**
     * 通过指定类型和字段查询条件
     * name: getSqlLike
     * @param $data
     * @return string
     * @Author:taiyang
     */
    public function getSqlLike($data=array()){
        $sql='';
        if($data['s_type_id'] == 1){//帖子id
            $sql.=" and tc.post_id= ".$data['s_content']." ";
        }elseif ($data['s_type_id'] == 2){//用户uid
            $sql.=" and tc.uid= ".$data['s_content']." ";
        }elseif ($data['s_type_id'] == 3){//帖子标题
            $sql.=" and tc.desc like '%".$data['s_content']."%' ";
        }elseif ($data['s_type_id'] == 4){//用户昵称
            $sql.=" and u.nickname like '%".$data['s_content']."%' ";
        }elseif ($data['s_type_id'] == 5){//帖子id
            $sql.=" and tc.id=".$data['s_content']." ";
        }
        return $sql;
    }

    /**
     * 通过指定类型查询
     * name: getSqlType
     * @param array $data
     * @return string
     * @Author:taiyang
     */
    public function getSqlType($data=array()){
        $sql='';
        if($data['post_type'] == 1){//茉莉导入
            $sql.=" and tc.type=2 and tc.source = '茉莉' ";
        }elseif($data['post_type'] == 2){//麻花导入
            $sql.=" and tc.type=2 and tc.source = '麻花' ";
        }elseif($data['post_type'] == 3){//用户发布
            $sql.=" and tc.type=1 ";
        }
        return $sql;
    }
    /**
     * 修改删评论时的统计
     * name: update_count
     * @param string $id
     * @param int $type
     * @return bool
     * @throws Exception
     * @Author:taiyang
     */
    public function update_count($id='',$type=1){
        if(empty($id)){return false;}
        //取出评论信息
        $comment_data=$this->query_one(array('id'=>$id));
        if(empty($comment_data)){return false;}
        if($type == 2){
            $a_type=24;
            $num="+1";
            $str="恢复评论";
        }else{
            $a_type=4;
            $num="-1";
            $str="删除评论";
            //task-event
            //触发任务埋点
            $this->taskEventApi('del_comment',array('uid'=>$comment_data['uid'],'data'=>'del_comment:'.$id));

        }
        //修改帖子评论数量 因为需求为删除后前台继续展示，所以此处不做数据更改>>>>改为数据减去，
        $this->PostModel->db->set('comment_num','comment_num'.$num,false)->where(array('id'=>$comment_data['post_id']));
        $this->PostModel->db->update('t_post');
        //修改用户送出的评论数
        $this->UserCount->db->set('reply_num','reply_num'.$num,false)->where(array('uid'=>$comment_data['uid']));
        $this->UserCount->db->update('t_user_count');
        //添加修改人信息
        $this->AdminOpenationModel->insertLog($id,$a_type,$str.'，评论id:'.$id);
        return true;
    }
}